package ade.prime.model;

import ade.prime.service.AdeWebService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * @author		Schnell Michaël
 * @version		1.0
 */
public class AdeWebBroadcaseReceiver extends BroadcastReceiver
implements IntentConstants {
	
	@SuppressWarnings("unused")
	private static final String TAG = AdeWebBroadcaseReceiver.class.getSimpleName() ;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction() ;
//		Log.d(TAG, String.format("[onReceive] action: '%s'", //$NON-NLS-1$
//				action)) ;
		if (action.equals(SCHEDULE_STATE_UPDATE_ACTION)
				|| action.equals(SCHEDULE_AUTO_UPDATE_ACTION)) {			
			Intent serviceIntent = new Intent(context, AdeWebService.class) ;
			serviceIntent.setAction(action) ;
			context.startService(serviceIntent) ;
		} else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
			boolean networkAvailable =
					!intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, 
							false) ;
			if (networkAvailable) {
				ConnectivityManager manager = (ConnectivityManager) 
						context.getSystemService(Context.CONNECTIVITY_SERVICE) ;
				NetworkInfo networkInfo = manager.getActiveNetworkInfo() ;
//				Log.i(TAG, "networkInfo: " + networkInfo) ;
				networkAvailable = networkInfo.isConnected() ;
			}// if
//			Log.i(TAG, "netWorkAvail: " + networkAvailable) ;
			AdeWebOptions.setNetworkAvailability(context, networkAvailable) ;
		}// if
	}// onReceive(Context, Intent)

}// class AdeWebBroadcaseReceiver